package com.foxzzz.two.pointers;


public class Solution28 {
    public int strStr(String haystack, String needle) {
        if (haystack.length() < needle.length()) {
            return -1;
        }
        if (needle.length() == 0) {
            return 0;
        }
        int j = 0;
        for (int i = 0; i < haystack.length(); ) {
            while (j < needle.length() && i < haystack.length()) {
                if (haystack.charAt(i) == needle.charAt(j)) {
                    j++;
                } else if (j != 0) {
                    i = i - j + 1;
                    j = 0;

                    continue;
                }
                if (j == needle.length()) {
                    return i - j + 1;
                }
                i++;
            }
            i++;

        }

        return -1;
    }

    public static void main(String[] args) {
        System.out.println(new Solution28().strStr("hello", "ll"));
        System.out.println(new Solution28().strStr("mississippi", "issip"));
        System.out.println(new Solution28().strStr("aaaaa", "bba"));
    }
}
